//
//  ZBDrawingView.m
//  ZBDrawingTool
//
//  Created by apple on 2018/6/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZBDrawingView.h"
#import "ZBBezierPathModel.h"

@interface ZBDrawingView()

//保存路径数组
@property (nonatomic, strong) NSMutableArray <ZBBezierPathModel *>*drawPaths;

@end

@implementation ZBDrawingView

#pragma mark - 操作事件

- (void)clean
{
    if(self.drawPaths.count == 0){
        return;
    }
    [self.drawPaths removeAllObjects];
    //重绘
    [self setNeedsDisplay];
    
}

- (void)back
{
    if(self.drawPaths.count == 0){
        return;
    }
    [self.drawPaths removeLastObject];
    //重绘
    [self setNeedsDisplay];
}

- (void)save
{
    if(self.drawPaths.count == 0){
        return;
    }
    UIImage *image = [self makeImageWithView:self withSize:self.bounds.size];
    //保存到相册
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishWithError:contextInfo:), nil);
}
- (UIImage *)getDrawingImage
{
    if(self.drawPaths.count == 0){
        return nil;
    }
    UIImage *image = [self makeImageWithView:self withSize:self.bounds.size];
    return image;
}

//保存图片的回调
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSString *message = @"";
    if (!error) {
        message = @"成功保存到相册";
    }else{
        message = [error description];
    }
    NSLog(@"message is %@",message);
}

#pragma mark - 重写系统的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1.获取触摸的任意对象
    UITouch *touch = [touches anyObject];
    
    //2.获取触摸的位置
    CGPoint point = [touch locationInView:touch.view];
    
    //3.创建起始点的路径
    ZBBezierPathModel *pathModel = [[ZBBezierPathModel alloc]init];
    
    
    //4.赋值开始点的属性
    if(self.lineWidth > 0){
      pathModel.path.lineWidth = self.lineWidth;
    }
    if(self.lineColor != nil){
      pathModel.lineColor = self.lineColor;
    }
    
    //5.设置起点
    [pathModel.path moveToPoint:point];
    
    //6.保存起点
    [self.drawPaths addObject:pathModel];
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1.获取触摸的任意对象
    UITouch *touch = [touches anyObject];
    
    //2.获取触摸的位置
    CGPoint point = [touch locationInView:touch.view];
    
    //3.移动过程链接点增加到数组里面去
    ZBBezierPathModel *pathModel  = self.drawPaths.lastObject;
    [pathModel.path addLineToPoint:point];
    
    //4.绘制
    [self setNeedsDisplay];
    
}

 - (void)drawRect:(CGRect)rect
{
    //绘制
    for (ZBBezierPathModel *pathModel in self.drawPaths) {
        //描绘颜色
        [pathModel.lineColor set];
        //渲染
        [pathModel.path stroke];
    }
    
}



#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    
    //获取上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //截屏
    [view.layer renderInContext:context];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片上下文
    UIGraphicsEndImageContext();
    return image;
}
    


#pragma mark - lazy
- (NSMutableArray <ZBBezierPathModel *>*)drawPaths
{
    if(_drawPaths == nil){
        _drawPaths =[ NSMutableArray array];
    }
    return _drawPaths;
}
@end
